﻿using System.Linq;
using e0 = CodeCamp.Entities00;
using e1 = CodeCamp.Entities01;

namespace CodeCamp.Controllers
{
  public class TransformEntities00ToEntities01
  {
    private CodeCampDataDataContext context00 = new CodeCampDataDataContext();
    private CodeCampData01DataContext context01 = new CodeCampData01DataContext();

    public void Execute()
    {
      e1.CurrentVersion currentVersion = context01.CurrentVersions.FirstOrDefault();
      if ( currentVersion.VersionNumber == 0 )
      {
        TransformEvents();
        currentVersion.VersionNumber = 1;
        context01.SubmitChanges();
      }
    }

    private void TransformEvents()
    {
      foreach ( e0.Event event00 in context00.Events )
      {
        // if event01 does not already exist, create it
        e1.Event event01 = context01.Events.Where( e => e.ShortName == event00.ShortName ).FirstOrDefault();
        if ( event01 == null )
        {
          event01 =
            new e1.Event
            {
              DisplayName = event00.DisplayName,
              ShortName = event00.ShortName
            };

          context01.Events.InsertOnSubmit( event01 );
          context01.SubmitChanges();
        }

        TransformPresenters( event00.Id, event01.Id );
        TransformSponsors( event00.Id, event01.Id );

        context00.Events.DeleteOnSubmit( event00 );
        context00.SubmitChanges();
      }
    }

    private void TransformPresenters( int event00Id, int event01Id )
    {
      var eventPresenters =
        from p in context00.Presenters
        where p.EventId == event00Id
        select p;

      foreach ( e0.Presenter presenter00 in eventPresenters )
      {
        // insert new presenter

        e1.Presenter presenter01 =
          new e1.Presenter
          {
            EventId = event01Id,
            UserIdentityName = presenter00.UserIdentityName,
            FirstName = presenter00.FirstName,
            LastName = presenter00.LastName,
            TwitterName = presenter00.TwitterName,
            EmailAddress = presenter00.EmailAddress,
            Biography = presenter00.Biography
          };

        context01.Presenters.InsertOnSubmit( presenter01 );
        context01.SubmitChanges();

        // transform presenter's sessions

        TransformSessions( event01Id, presenter00.Id, presenter01.Id );

        // delete old presenter

        context00.Presenters.DeleteOnSubmit( presenter00 );
        context00.SubmitChanges();
      }
    }

    private void TransformSessions( int event01Id, int presenter00Id, int presenter01Id )
    {
      var presenterSessions =
        from s in context00.Sessions
        where s.PresenterId == presenter00Id
        select s;

      foreach ( e0.Session session00 in presenterSessions )
      {
        // insert old session

        e1.Session session01 =
          new e1.Session
          {
            EventId = event01Id,
            Title = session00.Title,
            Description = session00.Description
          };

        context01.Sessions.InsertOnSubmit( session01 );
        context01.SubmitChanges();

        // insert new session presenter

        e1.SessionPresenter sessionPresenter01 =
          new e1.SessionPresenter
          {
            SessionId = session01.Id,
            PresenterId = presenter01Id
          };

        context01.SessionPresenters.InsertOnSubmit( sessionPresenter01 );
        context01.SubmitChanges();

        // delete old session

        context00.Sessions.DeleteOnSubmit( session00 );
        context00.SubmitChanges();
      }
    }

    private void TransformSponsors( int event00Id, int event01Id )
    {
      // insert new sponsors, delete old sponsors

      var eventSponsors =
        from s in context00.Sponsors
        where s.EventId == event00Id
        select s;

      foreach ( e0.Sponsor sponsor00 in eventSponsors )
      {
        e1.Sponsor sponsor01 =
          new e1.Sponsor
          {
            EventId = event01Id,
            Name = sponsor00.Name,
            Url = sponsor00.Url,
            ImageUrl = sponsor00.ImageUrl,
            ImageHeight = sponsor00.ImageHeight,
            ImageWidth = sponsor00.ImageWidth
          };

        context01.Sponsors.InsertOnSubmit( sponsor01 );
        context00.Sponsors.DeleteOnSubmit( sponsor00 );
      }

      context01.SubmitChanges();
      context00.SubmitChanges();
    }

  }
}